#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/timex.h>

static int value = 0;

module_param(value, int, 0644);
MODULE_PARM_DESC(value, "the beep frequences(20,32767)");

static __init int ioport_beep_init(void)
{
	unsigned int count = 0;

	if (value > 20 && value < 32767)
	      count = PIT_TICK_RATE / value;

	if (count) {
		/* set command for counter 2, write 2 bytes */
		outb_p(0xB6, 0x43);
		/* select desired HZ */
		outb_p(count & 0xff, 0x42); //write low 8 bits first
		outb((count >> 8) & 0xff, 0x42); //write hight 8 bits
		/* enable counter 2 */
		outb_p(inb_p(0x61) | 3, 0x61);
	} else {
		/* disable counter 2 */
		outb(inb_p(0x61) & 0xFC, 0x61);
	}

	return 0;
}

static __exit void ioport_beep_exit(void)
{
	/*disable counter 2 */
	outb(inb_p(0x61) & 0xFC, 0x61);

	printk("Bye ioport_beep!\n");
}

module_init(ioport_beep_init);
module_exit(ioport_beep_exit);

MODULE_LICENSE("GPL");
